package com.example.testinternet;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class AsyncActivity extends AppCompatActivity {
    ImageView ivPic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_async);
        ivPic= (ImageView) findViewById(R.id.iv_pic);
//        String ip="http://10.0.2.2:8080/pic/01.jpg";
        String ip="http://t.weather.sojson.com/api/weather/city/101080201";
        //1-创建okhttpclient
        OkHttpClient client=new OkHttpClient();
        //2-创建一个请求对象
        Request request=new Request.Builder().url(ip).build();
        //3-客户端创建请求
        Call call=client.newCall(request);
        //4-将请求添加到调度队列
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(Response response) throws IOException {
               // final Bitmap bitmap= BitmapFactory.decodeByteArray(response.body().bytes(),0, (int) response.body().contentLength());
//              runOnUiThread(new Runnable() {
//                  @Override
//                  public void run() {
//                      ivPic.setImageBitmap(bitmap);
//                  }
//              });
                Log.i("AsyncActivity", "onResponse: "+ response.body().string());



            }
        });

    }
}
